package com.kuaimacode.kframework.agent.controller.category;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.services.agent.SysAgentService;
import com.kuaimacode.kframework.agent.services.category.BuyIconService;
import com.kuaimacode.kframework.agent.services.category.CategoryService;
import com.kuaimacode.kframework.agent.util.SessionUtil;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.admin.SysAdminUser;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgent;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgentCategoryCfg;
import com.kuaimacode.kframework.mybatis.models.category.BuyIcon;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 分站物品品类设置 前端控制器
 * </p>
 *
 * @author Bean
 * @since 2019-03-17
 */
@Controller
@RequestMapping("/category")
public class CategoryController {

    private static String PAGE_FILE_NAME = "category/";

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SysAgentService agentService;

    @Autowired
    private BuyIconService buyIconService;


    @RequestMapping(value = {"/index"})
    public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
                        @RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
                        SysAgentCategoryCfg categoryCfg,ModelMap model) {
        //PageInfo<SysAgentCategoryCfg> pageInfo = categoryService.findAll(pageNum, pageSize,categoryCfg);
        // pageInfo.list 保存所有的品类
        //model.put("pageInfo", pageInfo);
        //model.put("categorySearch", categoryCfg);
        return PAGE_FILE_NAME + "index";
    }

    //查询平台默认品类设置
    @RequestMapping(value = {"/index_category/{type}"})
    public String categoryIndex(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
                                @RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
                                @PathVariable int type,ModelMap model) {
        SysAgentCategoryCfg categoryCfg = new SysAgentCategoryCfg();
        categoryCfg.setType(type);
        PageInfo<SysAgentCategoryCfg> pageInfo = categoryService.findAll(pageNum, pageSize,categoryCfg);
        // pageInfo.list categoryCfg
        model.put("pageInfo", pageInfo);
        model.put("categorySearch",categoryCfg);
        model.put("title",type==1?"帮我送":(type==2?"帮我取":"随意购"));
        return PAGE_FILE_NAME + "index_category";
    }



    @RequestMapping(value = "/add/{type}", method = RequestMethod.GET)
    public String add(@PathVariable int type,ModelMap map) {
        SysAgentCategoryCfg categoryCfg = new SysAgentCategoryCfg();
        categoryCfg.setInitiateKm(new BigDecimal(3));
        categoryCfg.setExceedKm(new BigDecimal(10.00));
        categoryCfg.setInitiatePrice(new BigDecimal(6.00));
        categoryCfg.setOverInitKmPrice(new BigDecimal(2.00));
        categoryCfg.setExceedExtraPrice(new BigDecimal(0.5));

        categoryCfg.setWeightBegin(3);
        categoryCfg.setWeightEnd(25);
        categoryCfg.setWeightExtraPrice(new BigDecimal(0.5));

        categoryCfg.setParcelInsuBegin(new BigDecimal(0));
        categoryCfg.setParcelInsuEnd(new BigDecimal(10000));
        categoryCfg.setParcelInsuRate(new BigDecimal(5));
        categoryCfg.setType(type);

//        categoryCfg.setDrawRate(new BigDecimal(70));//师傅提成比例
        categoryCfg.setCollectingServiceRate(new BigDecimal(10));//代收款服务费比例
        //查询未添加的分类
        List<SysAgentCategoryCfg> noAddList = categoryService.queryListByType(type+"");

        //查询随意购图标
        List<BuyIcon> buyIcons = buyIconService.findAll();
        map.put("icons",buyIcons);
        map.put("categoryCfg", categoryCfg);
        map.put("noAddList", noAddList);
        map.put("isAdd", true);
        map.put("title", type==1?"帮我送":(type==2?"帮我取":"随意购"));
        //获取当前分站 总站的提成比例
        SysAgent agent = agentService.getById(SessionUtil.getAgentId());
        map.put("platRate",agent.getProportion());
        map.put("maxRate",new BigDecimal(100).subtract(agent.getProportion()));
        return PAGE_FILE_NAME+"form";
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable String id, ModelMap map) {
        SysAgentCategoryCfg categoryCfg = categoryService.getById(id);
        SysAgentCategoryCfg categoryCfg1 = categoryService.getById(categoryCfg.getSysCategoryId());
        List<SysAgentCategoryCfg> noAddList = new ArrayList<>();
        noAddList.add(categoryCfg1);
        map.put("noAddList", noAddList);
        //查询随意购图标
        List<BuyIcon> buyIcons = buyIconService.findAll();
        map.put("icons",buyIcons);
        map.put("categoryCfg", categoryCfg);
        map.put("isAdd", false);

        SysAgent agent = agentService.getById(SessionUtil.getAgentId());
        map.put("platRate",agent.getProportion());
        map.put("maxRate",new BigDecimal(100).subtract(agent.getProportion()));
        return PAGE_FILE_NAME+"form";
    }

    @RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult edit(SysAgentCategoryCfg categoryCfg, Boolean isAdd,ModelMap map) {
        try {

            //查询图标信息
            BuyIcon buyIcon = buyIconService.getById(categoryCfg.getIcon());
            if(buyIcon!=null){
                categoryCfg.setIcon(buyIcon.getIcon());
                categoryCfg.setIconPre(buyIcon.getPreIcon());
            }
            categoryCfg.setUpdateBy(SessionUtil.getUserId());
            categoryCfg.setUpdateAt(new Date());
            if(isAdd){
                // 新建保存
                categoryCfg.setCreateBy(SessionUtil.getUserId());
                categoryCfg.setAgentId(SessionUtil.getAgentId());
                categoryCfg.setSysCategoryId(categoryCfg.getCategory());
                SysAgentCategoryCfg sysAgentCategoryCfg = categoryService.getById(categoryCfg.getCategory());
                categoryCfg.setCategory(sysAgentCategoryCfg.getCategory());
                categoryCfg.setCreateAt(new Date());
                categoryService.save(categoryCfg);
            }else{
                BigInteger id = BigInteger.valueOf(Long.parseLong(categoryCfg.getCategory()));
                SysAgentCategoryCfg categoryCfg1 = categoryService.getById(id);
                // 编辑保存
//                categoryCfg.setId(id);
                categoryCfg.setCategory(categoryCfg1.getCategory());
//                categoryCfg.setCategory(categoryService.getById());
                categoryService.updateById(categoryCfg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult delete(@PathVariable String id, ModelMap map) {
        try {
            categoryService.removeById(id);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }
    @RequestMapping(value = "/init", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult init(ModelMap map) {
        try {
            categoryService.init();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }



}
